package com.jhd.fuzhou.view.punishment;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.gson.Gson;
import com.jhd.fuzhou.view.login.BaseActivity;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.service.PunishmentService;
import com.jhd.fuzhou.service.SpecialService;
import com.jhd.fuzhou.service.SupervisionService;
import com.jhd.fuzhou.util.CustomToast;
import com.jhd.fuzhou.util.StringShow;
import com.jhd.fuzhou.util.UtilHelper;
import com.jhd.fuzhou.vo.AdministrativePunishmentVo;
import com.jhd.fuzhou.vo.CheckValue;
import com.jhd.fuzhou.vo.CountyVo;
import com.jhd.fuzhou.vo.GlobalDictionaryVo;
import com.jhd.fuzhou.vo.JsonForResult;
import com.jhd.fuzhou.vo.MajorSpeciality;
import com.jhd.fuzhou.vo.OrganizationVo;
import com.jhd.fuzhou.vo.PunishmentSearch;

import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.StringUtil;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

import static com.jhd.fuzhou.util.TimeUtil.getTimeDialog3;

/*行政处罚*/
public class AdministrativePunishmentActivity extends BaseActivity {

    @BindView(R.id.rv_status)RecyclerView rv_status;
    @BindView(R.id.rv_punishment)RecyclerView rv_punishment;
    @BindView(R.id.tv_loading)TextView tv_loading;
    private String[] statusList=new String[]{"待审核","已完成"};
    private Context context=this;
    private PunishmentSearch punishmentSearch=new PunishmentSearch();
    private int pageNum=0;
    private List<AdministrativePunishmentVo> administrativePunishmentVoList;
    private AlertDialog _alertDialog=null;
    private List<CountyVo> countyVoList;
    private List<OrganizationVo> organizationVoList;
    private ListView listView;
    private PopupWindow popup;
    private List<CheckValue> checkValueList;
    private List<MajorSpeciality> majorSpecialityList;
    private RecyclerView rv_classify,rv_acceptOrgan;
    private RecyclerView rv_progress,rv_source;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_administrative_punishment);
    }

    @Override
    protected void processLogic() {
        onBack();
        getMajorSpecialitySelector();getCountySelector();getValue();getOrgSelector();
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context);
        rv_punishment.setLayoutManager(linearLayoutManager);
        administrativePunishmentVoList=new ArrayList<>();
        punishmentSearch.setPageIndex(1);
        punishmentSearch.setIsFinish("0");
        GridLayoutManager gridLayoutManager=new GridLayoutManager(context,4);
        rv_status.setLayoutManager(gridLayoutManager);
        setStatus();
        rv_punishment.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastCompletelyVisibleItemPosition = linearLayoutManager.findLastCompletelyVisibleItemPosition();
                if(lastCompletelyVisibleItemPosition==linearLayoutManager.getItemCount()-1){
                    tv_loading.setVisibility(View.VISIBLE);
                    punishmentSearch.setPageIndex(punishmentSearch.getPageIndex()+1);
                    if(punishmentSearch.getPageIndex()<=pageNum){
                        tv_loading.setText(getString(R.string.loading));
                        getAppPunishmentList();
                    }else{
                        tv_loading.setText(getString(R.string.arrival_bottom));
                    }
                }else{
                    tv_loading.setVisibility(View.GONE);//没到底部，隐藏
                }
            }
        });
    }
    @OnClick({R.id.lin_select})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.lin_select:
                dialogShow();
                break;
        }
    }

    public void dialogShow() {
        punishmentSearch.setUnitName(null);punishmentSearch.setClassify(null);punishmentSearch.setAddress(null);
        punishmentSearch.setProgress(null);punishmentSearch.setAcceptOrgan(null);punishmentSearch.setSource(null);
        punishmentSearch.setProgressEsay(null);punishmentSearch.setStart(null);punishmentSearch.setEnd(null);
        LayoutInflater mLayoutInflater = LayoutInflater.from(this);
        View view = mLayoutInflater.inflate(R.layout.dialog_punishment, null);
        final AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context, R.style.ActionSheetDialogStyle);
        alertBuilder.setView(view);
        _alertDialog = alertBuilder.create();
        //获取当前Activity所在的窗体
        Window window = _alertDialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        _alertDialog.show();
        EditText et_search_dialog = (EditText) view.findViewById(R.id.et_search);
        ImageView iv_dismiss = (ImageView) view.findViewById(R.id.iv_dismiss);//顶部叉键
        ImageView iv_cancel = (ImageView) view.findViewById(R.id.iv_cancel);//搜索框有内容时的叉键
        Button btn_reset = (Button) view.findViewById(R.id.btn_reset);//重置
        Button btn_confirm = (Button) view.findViewById(R.id.btn_confirm);//确定
        LinearLayout lin_startTime = (LinearLayout) view.findViewById(R.id.lin_startTime);
        LinearLayout lin_endTime = (LinearLayout) view.findViewById(R.id.lin_endTime);
        LinearLayout lin_county = (LinearLayout) view.findViewById(R.id.lin_county);
        TextView tv_startTime = (TextView) view.findViewById(R.id.tv_startTime);
        TextView tv_registerAddress = (TextView) view.findViewById(R.id.tv_registerAddress);
        TextView tv_endTime = (TextView) view.findViewById(R.id.tv_endTime);
        rv_classify = (RecyclerView) view.findViewById(R.id.rv_classify);//所属专业
        rv_acceptOrgan = (RecyclerView) view.findViewById(R.id.rv_acceptOrgan);//查处机构
        rv_progress = (RecyclerView) view.findViewById(R.id.rv_progress);//案件进度
        rv_source = (RecyclerView) view.findViewById(R.id.rv_source);//案件来源
        RadioButton rb_progressEsay_1 = (RadioButton) view.findViewById(R.id.rb_progressEsay_1);//处罚程序,简易程序
        RadioButton rb_progressEsay_2 = (RadioButton) view.findViewById(R.id.rb_progressEsay_2);//处罚程序,一般程序
        RadioButton rb_result_1 = (RadioButton) view.findViewById(R.id.rb_result_1);//执行结果,完全履行
        RadioButton rb_result_2 = (RadioButton) view.findViewById(R.id.rb_result_2);//执行结果,不完全履行
        RadioButton rb_result_3 = (RadioButton) view.findViewById(R.id.rb_result_3);//执行结果,未履行
        rb_progressEsay_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                punishmentSearch.setProgressEsay("1");
            }
        });
        rb_progressEsay_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                punishmentSearch.setProgressEsay("2");
            }
        });
        rb_result_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                punishmentSearch.setResult("1");
            }
        });
        rb_result_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                punishmentSearch.setResult("2");
            }
        });
        rb_result_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                punishmentSearch.setResult("3");
            }
        });
        lin_county.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCountyVo(tv_registerAddress);
            }
        });
        lin_startTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getTimeDialog3(tv_startTime,context);
            }
        });
        lin_endTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getTimeDialog3(tv_endTime,context);
            }
        });

        showCountyVo(tv_registerAddress);
        setMajorButton(rv_classify);setOrgButton(rv_acceptOrgan);setProcessButton(rv_progress);setSourceButton(rv_source);

        btn_reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                punishmentSearch.setUnitName(null);punishmentSearch.setClassify(null);punishmentSearch.setAddress(null);
                punishmentSearch.setProgress(null);punishmentSearch.setAcceptOrgan(null);punishmentSearch.setSource(null);
                punishmentSearch.setProgressEsay(null);punishmentSearch.setStart(null);punishmentSearch.setEnd(null);
                tv_registerAddress.setText("全部地区");et_search_dialog.setText("");tv_startTime.setText("请选择");tv_endTime.setText("请选择");
                rb_progressEsay_1.setChecked(false);rb_progressEsay_2.setChecked(false);rb_result_1.setChecked(false);rb_result_2.setChecked(false);rb_result_3.setChecked(false);
                setMajorButton(rv_classify);setOrgButton(rv_acceptOrgan);setProcessButton(rv_progress);setSourceButton(rv_source);
            }
        });
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _alertDialog.dismiss();
                if(!tv_startTime.getText().toString().equals("请选择"))punishmentSearch.setStart(tv_startTime.getText().toString());
                if(!tv_endTime.getText().toString().equals("请选择"))punishmentSearch.setStart(tv_endTime.getText().toString());
                if(!et_search_dialog.getText().toString().trim().equals(""))punishmentSearch.setUnitName(et_search_dialog.getText().toString().trim());
                punishmentSearch.setPageIndex(1);
                administrativePunishmentVoList.clear();
                getAppPunishmentList();
            }
        });
        iv_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _alertDialog.dismiss();
            }
        });
        iv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                et_search_dialog.setText("");
                iv_cancel.setVisibility(View.GONE);
            }
        });
        et_search_dialog.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) { }
            @Override
            public void afterTextChanged(Editable s) {
                if(!s.toString().equals("")){
                    iv_cancel.setVisibility(View.VISIBLE);
                }else{
                    iv_cancel.setVisibility(View.GONE);
                }
            }
        });
        WindowManager.LayoutParams params = window.getAttributes() ;
        params.width =(int) (getWindowManager().getDefaultDisplay().getWidth());                     //使用这种方式更改了dialog的框宽
        window.setAttributes(params);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if(administrativePunishmentVoList!=null){
            administrativePunishmentVoList.clear();
        }
        punishmentSearch.setPageIndex(1);
        punishmentSearch.setIsFinish("0");
        punishmentSearch.setUnitName(null);punishmentSearch.setClassify(null);punishmentSearch.setAddress(null);
        punishmentSearch.setProgress(null);punishmentSearch.setAcceptOrgan(null);punishmentSearch.setSource(null);
        punishmentSearch.setProgressEsay(null);punishmentSearch.setStart(null);punishmentSearch.setEnd(null);
        getAppPunishmentList();
    }

    public void setStatus(){
        StatusAdapter statusAdapter=new StatusAdapter();
        rv_status.setAdapter(statusAdapter);
    }
    public class StatusAdapter extends RecyclerView.Adapter<StatusAdapter.ViewHolder> {
        private int checkPosition=0;
        @NonNull
        @Override
        public StatusAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_radio3, parent, false);
            StatusAdapter.ViewHolder viewHolder = new StatusAdapter.ViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull StatusAdapter.ViewHolder holder, int position) {
            holder.rb_radio.setText(statusList[position]);
            holder.rb_radio.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    checkPosition=position;
                    notifyDataSetChanged();
                    //将弹窗筛选条件清空
                    punishmentSearch.setUnitName(null);punishmentSearch.setClassify(null);punishmentSearch.setAddress(null);
                    punishmentSearch.setProgress(null);punishmentSearch.setAcceptOrgan(null);punishmentSearch.setSource(null);
                    punishmentSearch.setProgressEsay(null);punishmentSearch.setStart(null);punishmentSearch.setEnd(null);
                    if(checkPosition==0){
                        punishmentSearch.setIsFinish("0");
                    }else if(checkPosition==1){
                        punishmentSearch.setIsFinish("1");
                    }
                    punishmentSearch.setPageIndex(1);
                    administrativePunishmentVoList.clear();
                    getAppPunishmentList();
                }
            });
            if(position==checkPosition){
                holder.rb_radio.setChecked(true);
            }else{
                holder.rb_radio.setChecked(false);
            }
        }

        @Override
        public int getItemCount() {
            return statusList.length;
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            RadioButton rb_radio;
            public ViewHolder(View itemView) {
                super(itemView);
                this.rb_radio = itemView.findViewById(R.id.rb_radio);
            }
        }
    }
    private void getValue(){
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.UC());//URL的头
        SupervisionService vs = retrofit.create(SupervisionService.class);
        JSONArray jsonArray=new JSONArray();
        jsonArray.add("案件进度");jsonArray.add("案件来源");/*jsonArray.add("送达方式");jsonArray.add("执行方式");jsonArray.add("执行结果");*/
        Map<String, Object> params = new LinkedHashMap<String, Object>();
        params.put("nameList",jsonArray);
        RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json"),
                JSON.toJSONString(params));
        Call<JsonForResult> jsonForResultCall = vs.getValueList(application.getAccessToken(), requestBody);
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    JsonForResult jsonForResult = response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {
                        //请求授权信息
                        if(jsonForResult.getData()!=null){
                            checkValueList= JSON.parseArray(jsonForResult.getData().toString(), CheckValue.class);
                        }
                    }
                } catch (Exception e) {
                    UtilHelper.alertMsg(context, "错误", e.getMessage());
                }
            }
            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "获取检测项目失败!";
                UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }
    public void getAppPunishmentList(){
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.API());//URL的头
        PunishmentService vs = retrofit.create(PunishmentService.class);
        Map<String, Object> params = new LinkedHashMap<String, Object>();
        params.put("pageIndex",punishmentSearch.getPageIndex());
        params.put("pageSize",punishmentSearch.getPageSize());
        params.put("isFinish",punishmentSearch.getIsFinish());
        params.put("userName",application.getUserName());
        if(punishmentSearch.getAddress()!=null)params.put("address",punishmentSearch.getAddress());
        if(punishmentSearch.getClassify()!=null)params.put("classify",punishmentSearch.getClassify());
        if(punishmentSearch.getProgress()!=null)params.put("progress",punishmentSearch.getProgress());
        if(punishmentSearch.getResult()!=null)params.put("result",punishmentSearch.getResult());
        if(punishmentSearch.getUnitName()!=null)params.put("unitName",punishmentSearch.getUnitName());
        if(punishmentSearch.getAcceptOrgan()!=null)params.put("acceptOrgan",punishmentSearch.getAcceptOrgan());
        if(punishmentSearch.getSource()!=null)params.put("source",punishmentSearch.getSource());
        if(punishmentSearch.getProgressEsay()!=null)params.put("progressEsay",punishmentSearch.getProgressEsay());
        if(punishmentSearch.getStart()!=null)params.put("start",punishmentSearch.getStart());
        if(punishmentSearch.getEnd()!=null)params.put("end",punishmentSearch.getEnd());
        Call<JsonForResult> jsonForResultCall = vs.getAppPunishmentList(application.getAccessToken(),params);//调用接口获取json返回
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    JsonForResult jsonForResult = (JsonForResult) response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {
                        if(jsonForResult.getData()!=null){
                            List<AdministrativePunishmentVo> administrativePunishmentVos= JSON.parseArray(jsonForResult.getData().toString(), AdministrativePunishmentVo.class);
                            if(administrativePunishmentVoList.size()==0&&administrativePunishmentVos.size()==0){
                                CustomToast.showText(context,"数据为空！",3000,0,0).show();
                            }else{
                                for (AdministrativePunishmentVo administrativePunishmentVo : administrativePunishmentVos) {
                                    administrativePunishmentVoList.add(administrativePunishmentVo);
                                }
                            }
                        }else{
                            CustomToast.showText(context,"数据为空！",3000,0,0).show();
                        }
                        AdministrativePunishmentAdapter adapter=new AdministrativePunishmentAdapter();
                        rv_punishment.setAdapter(adapter);
                        //判断可以分多少页
                        if((jsonForResult.getTotal()%punishmentSearch.getPageSize())!=0){
                            pageNum=(jsonForResult.getTotal()/ punishmentSearch.getPageSize())+1;
                        }else{
                            pageNum=(jsonForResult.getTotal()/ punishmentSearch.getPageSize());
                        }
                    } else {
                        UtilHelper.alertMsg(context, "错误", jsonForResult.getMessage());
                    }
                } catch (Exception e) {
                    CustomToast.showText(context,e.getLocalizedMessage(),3000,0,0).show();
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "网络异常!";
                if (StringUtil.isNullOrEmpty(t.getMessage()) == false) msg = msg + t.getMessage();
                UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }
    public  class AdministrativePunishmentAdapter extends RecyclerView.Adapter<AdministrativePunishmentAdapter.ViewHolder> {
        @Override
        public AdministrativePunishmentAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_sy, parent, false);
            ViewHolder viewHolder = new ViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull AdministrativePunishmentAdapter.ViewHolder holder, int position) {
            holder.tv_taskName.setText(StringShow.showStr(administrativePunishmentVoList.get(position).getUnitName()));
            if(StringUtil.isNullOrEmpty(administrativePunishmentVoList.get(position).getCreateTime())){
                holder.tv_date.setText("");
            }else{
                holder.tv_date.setText(administrativePunishmentVoList.get(position).getCreateTime().substring(0,11));
            }
//            holder.tv_dept.setText(StringShow.showStr(supervisionInspectionVoList.get(position).getSupervisedUnitName()));
            holder.tv_dept.setText(StringShow.showStr(administrativePunishmentVoList.get(position).getAcceptOrgan()));
            holder.tv_status.setText((punishmentSearch.getIsFinish().equals("0")?"待审核":"已完成"));
            holder.tv_task_type.setText(StringShow.showStr(administrativePunishmentVoList.get(position).getProgress()));
            holder.linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context, AdministrativeDetailActivity.class);
                    Gson gson = new Gson();
                    intent.putExtra("isFinish",punishmentSearch.getIsFinish());
                    intent.putExtra("data", gson.toJson(administrativePunishmentVoList.get(position)));
                    context.startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return administrativePunishmentVoList.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tv_date,tv_taskName,tv_dept,tv_task_type,tv_status;
            LinearLayout linearLayout;
            public ViewHolder(View itemView) {
                super(itemView);
                this.tv_date = itemView.findViewById(R.id.tv_date);
                this.tv_taskName = itemView.findViewById(R.id.tv_taskName);
                this.tv_dept = itemView.findViewById(R.id.tv_dept);
                this.tv_task_type = itemView.findViewById(R.id.tv_task_type);
                this.tv_status = itemView.findViewById(R.id.tv_status);
                this.linearLayout = itemView.findViewById(R.id.linearLayout);
            }
        }
    }

    //获取所属专业列表
    private void getCountySelector(){
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.API());//URL的头
        SpecialService vs = retrofit.create(SpecialService.class);
        Map<String, String> params = ParamsMapUtil.getInstance("cityCode","361000");
        Call<JsonForResult> jsonForResultCall = vs.getCountySelector(application.getAccessToken(),params);//调用接口获取json返回
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    JsonForResult jsonForResult = (JsonForResult) response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {
                        countyVoList = JSON.parseArray(jsonForResult.getData().toString(), CountyVo.class);
                    } else {
                        UtilHelper.alertMsg(context, "错误", jsonForResult.getMessage());
                    }
                } catch (Exception e) {
                    CustomToast.showText(context,e.getLocalizedMessage(),3000,0,0).show();
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "网络错误!";
                if (StringUtil.isNullOrEmpty(t.getMessage()) == false) msg = msg + t.getMessage();
                UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }
    private void showCountyVo(TextView textView) {
        listView = new ListView(context);
        listView.setDivider(null);//设置分割线
        listView.setVerticalScrollBarEnabled(false);//不显示滑动条
        listView.setAdapter(new CountyVoAdapter(textView));
        popup = new PopupWindow(context);
        popup.setWidth(textView.getWidth()*2);
        popup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popup.setContentView(listView);//设置显示内容
        popup.setOutsideTouchable(true);//点击PopupWindow以外的区域自动关闭该窗口
        popup.showAsDropDown(textView, 0, 15);//显示在edit控件的下面0,0代表偏移量
    }
    private class CountyVoAdapter extends BaseAdapter {
        private int checkPosition=-1;private TextView textView;
        public CountyVoAdapter(TextView textView) {
            this.textView = textView;
        }
        @Override
        public int getCount() {
            return countyVoList.size();
        }
        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            CountyVoAdapter.HolderView holderView = null;
            if (convertView == null) {
                convertView = View.inflate(context, R.layout.item_radio, null);
                holderView = new CountyVoAdapter.HolderView();
                holderView.rb_radio = (RadioButton) convertView.findViewById(R.id.rb_radio);
                convertView.setTag(holderView);
            } else {
                holderView = (CountyVoAdapter.HolderView) convertView.getTag();
            }

            holderView.rb_radio.setText(countyVoList.get(position).getName());
            holderView.rb_radio.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    checkPosition=position;
                    textView.setText(countyVoList.get(position).getName());
                    punishmentSearch.setAddress(countyVoList.get(position).getCityName()+countyVoList.get(position).getName());
                    popup.dismiss();
                }
            });
            return convertView;
        }
        class HolderView {//由于这里用户都是同一个人,就省略用户图像的刷新
            private RadioButton rb_radio;
        }
    }

    //获取获取监管机构
    private void getOrgSelector(){
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.API());//URL的头
        SpecialService vs = retrofit.create(SpecialService.class);
        Call<JsonForResult> jsonForResultCall = vs.getOrgSelector(application.getAccessToken());//调用接口获取json返回
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    JsonForResult jsonForResult = (JsonForResult) response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {
                        organizationVoList = JSON.parseArray(jsonForResult.getData().toString(), OrganizationVo.class);
                        //setSuperviseDeptButton(rv_superviseDept);
                    } else {
                        UtilHelper.alertMsg(context, "错误", jsonForResult.getMessage());
                    }
                } catch (Exception e) {
                    CustomToast.showText(context,e.getLocalizedMessage(),3000,0,0).show();
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "网络错误!";
                if (StringUtil.isNullOrEmpty(t.getMessage()) == false) msg = msg + t.getMessage();
                UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }
    /*设置监管部门*/
    public void setMajorButton(RecyclerView recyclerView){
        GridLayoutManager layoutManage = new GridLayoutManager(context, 2);
        recyclerView.setLayoutManager(layoutManage);
        if( majorSpecialityList!=null){
            ButtonAdapter recyclerAdapter=new ButtonAdapter((List<Object>)(List)majorSpecialityList,1);
            recyclerView.setAdapter(recyclerAdapter);
        }
    }

    public void setOrgButton(RecyclerView recyclerView){
        GridLayoutManager layoutManage = new GridLayoutManager(context, 2);
        recyclerView.setLayoutManager(layoutManage);
        if(organizationVoList !=null){
            ButtonAdapter recyclerAdapter=new ButtonAdapter((List<Object>)(List)organizationVoList,2);
            recyclerView.setAdapter(recyclerAdapter);
        }
    }
    public void setProcessButton(RecyclerView recyclerView){
        for (CheckValue checkValue : checkValueList) {
            if (checkValue.getName().equals("案件进度")) {
                List<GlobalDictionaryVo> globalDictionaryVos = checkValue.getGlobalDictionaryVoMap();
                GridLayoutManager layoutManage = new GridLayoutManager(context, 2);
                recyclerView.setLayoutManager(layoutManage);
                if(globalDictionaryVos !=null){
                    ButtonAdapter recyclerAdapter=new ButtonAdapter((List<Object>)(List)globalDictionaryVos,3);
                    recyclerView.setAdapter(recyclerAdapter);
                }

                break;
            }
        }
    }
    public void setSourceButton(RecyclerView recyclerView){
        for (CheckValue checkValue : checkValueList) {
            if (checkValue.getName().equals("案件来源")) {
                List<GlobalDictionaryVo> globalDictionaryVos = checkValue.getGlobalDictionaryVoMap();
                GridLayoutManager layoutManage = new GridLayoutManager(context, 2);
                recyclerView.setLayoutManager(layoutManage);
                if(globalDictionaryVos !=null){
                    ButtonAdapter recyclerAdapter=new ButtonAdapter((List<Object>)(List)globalDictionaryVos,4);
                    recyclerView.setAdapter(recyclerAdapter);
                }
                break;
            }
        }
    }

    //获取所属专业列表
    private void getMajorSpecialitySelector(){
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.API());//URL的头
        SupervisionService vs = retrofit.create(SupervisionService.class);
        Call<JsonForResult> jsonForResultCall = vs.getMajorSpecialitySelector(application.getAccessToken());//调用接口获取json返回
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    JsonForResult jsonForResult = (JsonForResult) response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {
                        majorSpecialityList = JSON.parseArray(jsonForResult.getData().toString(), MajorSpeciality.class);
                    } else {
                        UtilHelper.alertMsg(context, "错误", jsonForResult.getMessage());
                    }
                } catch (Exception e) {
                    CustomToast.showText(context,e.getLocalizedMessage(),3000,0,0).show();
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "网络错误!";
                if (StringUtil.isNullOrEmpty(t.getMessage()) == false) msg = msg + t.getMessage();
                UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }

    public class ButtonAdapter extends RecyclerView.Adapter<ButtonAdapter.ViewHolder> {
        private List<OrganizationVo> organizationVoList;
        private List<MajorSpeciality> majorSpecialityList;
        private List<GlobalDictionaryVo> globalDictionaryVos;
        private List<Object> objects;
        private int type;
        private int checkPosition=-1;

        public ButtonAdapter(List<Object> list,int type) {
            this.objects=list;
            this.type=type;
            if(type==1){//查处机构
                this.majorSpecialityList = (List<MajorSpeciality>)(List)list;
            }else if(type==2){
                this.organizationVoList = (List<OrganizationVo>)(List)list;
            }else if(type==3||type==4){//案件进度,案件来源
                this.globalDictionaryVos = (List<GlobalDictionaryVo>)(List)list;
            }
        }

        @NonNull
        @Override
        public ButtonAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_radio, parent, false);
            ButtonAdapter.ViewHolder viewHolder = new ButtonAdapter.ViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull ButtonAdapter.ViewHolder holder, int position) {
            if(type==1){
                holder.rb_radio.setText(majorSpecialityList.get(position).getName());
                holder.rb_radio.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(checkPosition==position){
                            holder.rb_radio.setChecked(false);
                            checkPosition=-1;
                            punishmentSearch.setClassify(null);
                        }else {
                            checkPosition=position;
                            punishmentSearch.setClassify(majorSpecialityList.get(position).getName());
                        }
                        notifyDataSetChanged();
                    }
                });
            }else if(type==2){
                holder.rb_radio.setText(organizationVoList.get(position).getName());
                holder.rb_radio.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(checkPosition==position){
                            holder.rb_radio.setChecked(false);
                            checkPosition=-1;
                            punishmentSearch.setAcceptOrgan(null);
                        }else {
                            checkPosition=position;
                            punishmentSearch.setAcceptOrgan(organizationVoList.get(position).getName());
                        }
                        notifyDataSetChanged();
                    }
                });
            }else if(type==3){
                holder.rb_radio.setText(globalDictionaryVos.get(position).getValue());
                holder.rb_radio.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(checkPosition==position){
                            holder.rb_radio.setChecked(false);
                            checkPosition=-1;
                            punishmentSearch.setProgress(null);
                        }else {
                            checkPosition=position;
                            punishmentSearch.setProgress(globalDictionaryVos.get(position).getValue());
                        }
                        notifyDataSetChanged();
                    }
                });
            }else if(type==4){
                holder.rb_radio.setText(globalDictionaryVos.get(position).getValue());
                holder.rb_radio.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(checkPosition==position){
                            holder.rb_radio.setChecked(false);
                            checkPosition=-1;
                            punishmentSearch.setSource(null);
                        }else {
                            checkPosition=position;
                            punishmentSearch.setSource(globalDictionaryVos.get(position).getKey());
                        }
                        notifyDataSetChanged();
                    }
                });
            }
            if(position==checkPosition){
                holder.rb_radio.setChecked(true);
            }else{
                holder.rb_radio.setChecked(false);
            }
        }

        @Override
        public int getItemCount() {
           return objects.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            RadioButton rb_radio;
            public ViewHolder(View itemView) {
                super(itemView);
                this.rb_radio = itemView.findViewById(R.id.rb_radio);
            }
        }
    }
}